<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/6/21
 * Time: 下午4:17
 */

namespace app\index\service;


use app\index\model\News;
use app\index\model\SubCategory;
use app\index\model\SupCategory;

class NewsService
{
    const REC_NEWS_KEY = "rec_news_list_key";
    const NEWS_INDEX_KEY = "news_index_key";
    const HOT_NEWS_KEY = "hot_news_list_key";
    const NEWS_DETAIL_KEY = "news_detail_key";
    const NEWS_LIST_KEY = "news_list_key";
    const NEWS_SUB_CATEGORY_LIST_KEY = "news_sub_cateogry_list_key";
    const EXPIRE_IN = 60*5;

    /**
     * 获取首页推荐新闻
     * @return array|mixed
     */
    public static function getIndexRecNewsList(){
        $news_list = cache(self::REC_NEWS_KEY);
        if($news_list){
            return $news_list;
        }
        $data = News::getRecList();
        cache(self::REC_NEWS_KEY, $data, 5*60);
        return $data;
    }
    public static function getNewsIndexData(){
        $news_index_list = cache(self::NEWS_INDEX_KEY);
        if($news_index_list){
            return $news_index_list;
        }
        $data = News::getNewsIndexList();
        cache(self::NEWS_INDEX_KEY, $data, self::EXPIRE_IN);
        return $data;
    }
    public static function getHotNewsList(){
        $hot_news_list = cache(self::HOT_NEWS_KEY);
        if($hot_news_list){
            return $hot_news_list;
        }
        $data = News::getHotList();
        cache(self::HOT_NEWS_KEY,$data,self::EXPIRE_IN);
        return $data;
    }
    public static function getNewsDetail($id){
        $cache_detail = cache(self::NEWS_DETAIL_KEY.$id);
        if($cache_detail){
            return $cache_detail;
        }
        $detail = News::getOne($id);
        $last_id = News::getLastId($id);
        $next_id = News::getNextId($id);
        $last_news = News::getOne($last_id);
        $next_news = News::getOne($next_id);
        if($last_news == null){
            $last_news["id"] = 0;
            $last_news["title"] = "没有上一条了";

        }
        if($next_news ==null){
            $next_news["id"] = 0;
            $next_news["title"] = "没有下一条了";

        }
        $recommend = News::getRecList(2);
        $ret = [
            "detail"=>$detail,
            "last_news"=>$last_news,
            "next_news"=>$next_news,
            "recommend"=>$recommend
        ];
        cache(self::NEWS_DETAIL_KEY.$id,$ret,5*60);
        return $ret;
    }
    public static function getNewsList($page){
        $cache_news_list = cache(self::NEWS_LIST_KEY.$page);
        if($cache_news_list){
            return $cache_news_list;
        }
        $data = News::getList($page,10);
        cache(self::NEWS_LIST_KEY.$page,$data,60*5);
        return $data;
    }
    public static function getSubCategorysByNews(){
        $cache_sub_categorys = cache(self::NEWS_SUB_CATEGORY_LIST_KEY);
        if($cache_sub_categorys){
            return $cache_sub_categorys;
        }
        $data = News::getSubCategoryIDs();
        $subcategory_ids = [];
        foreach ($data as $item){
            array_push($subcategory_ids,$item['subcategory_id']);
        }
        $subcategory_ids = array_unique($subcategory_ids);
        $sub_categorys = [];

        $count = 0;
        foreach ($subcategory_ids as $id){
            $category_item["id"] = $id;
            $category_item["category_name"] = SubCategory::getCategoryNameById($id);
            array_push($sub_categorys,$category_item);
            $count++;
            if($count >=6){
                break;
            }
        }
        $category_item["id"] = 0;
        $category_item["category_name"] = "不限";
        array_unshift($sub_categorys,$category_item);
        cache(self::NEWS_SUB_CATEGORY_LIST_KEY,$sub_categorys,self::EXPIRE_IN);
        return $sub_categorys;

    }

}